package com.ap.core.validation.message.rules;

import com.ap.core.cxf.jaxb.msghdr.MessageType;
import com.ap.core.dao.FinancialInstitutionDao;
import com.ap.core.domain.FinancialInstitution;
import com.ap.core.message.info.RequestMessageInfo;
import com.ap.core.message.manageconsumer.info.RequestRegisterConsumerTypeInfo;
import com.ap.core.message.manageconsumer.info.RequestRetrieveConsumerDetailsTypeInfo;
import com.ap.core.validation.message.info.MessageValidationInfo;
import com.ap.core.validation.message.info.RTPOriginalInfo;
import com.ap.core.validation.rules.AbstractValidationRule;

/**
 * User: Ranjit.Singh
 * Check if provided FI ID is valid
 */
public class ValidFIIdRule extends AbstractValidationRule<MessageValidationInfo<RequestMessageInfo<MessageType>, RTPOriginalInfo>> {

    private FinancialInstitutionDao financialInstitutionDao;
    private FinancialInstitution financialInstitution;
    
    @Override
    public boolean validate(MessageValidationInfo<RequestMessageInfo<MessageType>, RTPOriginalInfo> infoObject) {
    	if(infoObject.getBoundObject() instanceof RequestRegisterConsumerTypeInfo) {
            financialInstitution = financialInstitutionDao.getFIByIdentification(((RequestRegisterConsumerTypeInfo) infoObject.getBoundObject()).getRegistrationInfo().getFIId());
        } else if(infoObject.getBoundObject() instanceof RequestRetrieveConsumerDetailsTypeInfo) {
        	 financialInstitution = financialInstitutionDao.getFIByIdentification(((RequestRetrieveConsumerDetailsTypeInfo)infoObject.getBoundObject()).getConsRegRequestInfo().getFIId());
        }
        return null != financialInstitution;
    }

    public void setFinancialInstitutionDao(FinancialInstitutionDao financialInstitutionDao) {
        this.financialInstitutionDao = financialInstitutionDao;
    }
}
